import {createSlice} from '@reduxjs/toolkit'

//创建 slice 对象
const slice = createSlice({
  //action type 的别名   action type
  name: 'count',
  //状态的初始值
  initialState: 100, 
  //配置 reducer 的功能
  reducers: {
    // {type: 'incre', payload: 1}
    incre: (state, action) => {
      return state + action.payload;
    },
    //减少
    decre: (state, action) => {
      return state - action.payload;
    }
  }
});

//获得 action creator 函数      let increAction = (payload) => ({type: 'incre', payload: payload})
export let {incre, decre} = slice.actions;

//获取 reducer 函数
let countReducer = slice.reducer;

//声明异步的 action creator
export function asyncIncreAction(payload){
  return dispatch => {
    setTimeout(() => {
      dispatch(incre(payload))
    }, 1000);
  }
}

export default countReducer;